package com.haitaoit.chengquan.module.center.adapter.order;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BackCall;
import com.haitaoit.chengquan.module.center.model.MyOrderModel;
import com.haitaoit.chengquan.view.MyListView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.List;

/**
 * Created by Administrator on 2016/10/13.
 */
public class MyOrderAdapter extends BaseAdapter {
    private Context context;
    private BackCall backCall;
    private List<MyOrderModel> list;

    public void setBackCall(BackCall backCall) {
        this.backCall = backCall;
    }

    public MyOrderAdapter(Context context) {
        this.context = context;
    }

    public List<MyOrderModel> getList() {
        return list;
    }

    public void setList(List<MyOrderModel> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.my_order_item, null);
            holder = new ViewHolder();
            ViewUtils.inject(holder, convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.order_itemll:
                        MyOrderAdapter.this.backCall.deal(R.id.order_itemll, position);
                        break;
                    case R.id.cancel_order:
                        MyOrderAdapter.this.backCall.deal(R.id.cancel_order, position);
                        break;
                    case R.id.go_pay:
                        MyOrderAdapter.this.backCall.deal(R.id.go_pay, position);
                        break;
                    case R.id.apply_refund:
                        MyOrderAdapter.this.backCall.deal(R.id.apply_refund, position);
                        break;
                    case R.id.remind_goods:
                        MyOrderAdapter.this.backCall.deal(R.id.remind_goods, position);
                        break;
                    case R.id.check_wuliu:
                        MyOrderAdapter.this.backCall.deal(R.id.check_wuliu, position);
                        break;
                    case R.id.confirm_complete:
                        MyOrderAdapter.this.backCall.deal(R.id.confirm_complete, position);
                        break;
                    case R.id.apply_return_goods:
                        MyOrderAdapter.this.backCall.deal(R.id.apply_return_goods, position);
                        break;
                    case R.id.apply_change_goods:
                        MyOrderAdapter.this.backCall.deal(R.id.apply_change_goods, position);
                        break;
                    case R.id.go_eva:
                        MyOrderAdapter.this.backCall.deal(R.id.go_eva, position);
                        break;
                    case R.id.detele_order:
                        MyOrderAdapter.this.backCall.deal(R.id.detele_order, position);
                        break;
                }
            }
        };
        holder.order_itemll.setOnClickListener(listener);
        holder.cancel_order.setOnClickListener(listener);
        holder.go_pay.setOnClickListener(listener);
        holder.apply_refund.setOnClickListener(listener);
        holder.apply_change_goods.setOnClickListener(listener);
        holder.remind_goods.setOnClickListener(listener);
        holder.check_wuliu.setOnClickListener(listener);
        holder.confirm_complete.setOnClickListener(listener);
        holder.apply_return_goods.setOnClickListener(listener);
        holder.go_eva.setOnClickListener(listener);
        holder.detele_order.setOnClickListener(listener);

        holder.cancel_order.setVisibility(View.GONE);// 取消订单
        holder.go_pay.setVisibility(View.GONE);// 付款
        holder.remind_goods.setVisibility(View.GONE);// 提醒发货
        holder.check_wuliu.setVisibility(View.GONE);// 查看物流
        holder.confirm_complete.setVisibility(View.GONE);// 确认收货
        holder.apply_return_goods.setVisibility(View.GONE);// 申请退货
        holder.apply_refund.setVisibility(View.GONE);// 申请退款
        holder.apply_change_goods.setVisibility(View.GONE);// 申请换货
        holder.go_eva.setVisibility(View.GONE);// 去评价
        holder.detele_order.setVisibility(View.GONE);// 删除订单

        /**
         * status:订单状态(1待付款显示取消订单和去付款
         * 2待发货显示申请退款和提醒发货 3待收货显示查看物流和确认收货
         * 4待评价显示申请退货和申请换货和评价 5交易成功显示申请退货和申请换货和删除订单)
         */
        if (list.get(position).getStatus().equals("1")) {
            holder.order_state.setText("待付款");
            holder.cancel_order.setVisibility(View.VISIBLE);// 取消订单
            holder.go_pay.setVisibility(View.VISIBLE);// 付款
        } else if (list.get(position).getStatus().equals("2")) {
            holder.order_state.setText("待发货");
            holder.apply_refund.setVisibility(View.VISIBLE);// 申请退款
            holder.remind_goods.setVisibility(View.VISIBLE);// 提醒发货
        } else if (list.get(position).getStatus().equals("3")) {
            holder.order_state.setText("待收货");
            holder.check_wuliu.setVisibility(View.VISIBLE);// 查看物流
            holder.confirm_complete.setVisibility(View.VISIBLE);// 确认收货
        } else if (list.get(position).getStatus().equals("4")) {
            holder.order_state.setText("待评价");
            holder.apply_return_goods.setVisibility(View.VISIBLE);// 申请退货
            holder.apply_change_goods.setVisibility(View.VISIBLE);// 申请换货
            holder.go_eva.setVisibility(View.VISIBLE);// 去评价
        } else if (list.get(position).getStatus().equals("5")) {
            holder.order_state.setText("已完成");
            holder.apply_return_goods.setVisibility(View.VISIBLE);// 申请退货
            holder.apply_change_goods.setVisibility(View.VISIBLE);// 申请换货
            holder.detele_order.setVisibility(View.VISIBLE);// 删除订单
        }
        holder.order_no.setText("订单号：" + list.get(position).getOrder_no());
        holder.order_goods_count.setText("总计" + list.get(position).getOrder_goods_count() + "件商品");
        holder.order_amount.setText("¥ " + list.get(position).getOrder_amount());
        holder.order_logistics_money.setText("（含运费 ¥ " + list.get(position).getOrder_logistics_money() + "）");

        MyOrderItemAdapter adapter2 = new MyOrderItemAdapter(context);
        adapter2.setList(list.get(position).getList_goods());
        holder.scrollview_listview.setAdapter(adapter2);
        holder.scrollview_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
                MyOrderAdapter.this.backCall.deal(R.id.scrollview_listview, position);
            }
        });
        return convertView;
    }

    class ViewHolder {
        @ViewInject(R.id.order_itemll)
        LinearLayout order_itemll;
        @ViewInject(R.id.scrollview_listview)
        MyListView scrollview_listview;
        @ViewInject(R.id.order_state)
        TextView order_state;
        @ViewInject(R.id.cancel_order)
        TextView cancel_order;
        @ViewInject(R.id.go_pay)
        TextView go_pay;
        @ViewInject(R.id.apply_refund)
        TextView apply_refund;
        @ViewInject(R.id.remind_goods)
        TextView remind_goods;
        @ViewInject(R.id.check_wuliu)
        TextView check_wuliu;
        @ViewInject(R.id.confirm_complete)
        TextView confirm_complete;
        @ViewInject(R.id.apply_return_goods)
        TextView apply_return_goods;
        @ViewInject(R.id.apply_change_goods)
        TextView apply_change_goods;
        @ViewInject(R.id.go_eva)
        TextView go_eva;
        @ViewInject(R.id.detele_order)
        TextView detele_order;
        @ViewInject(R.id.order_no)
        TextView order_no;
        @ViewInject(R.id.order_goods_count)
        TextView order_goods_count;
        @ViewInject(R.id.order_amount)
        TextView order_amount;
        @ViewInject(R.id.order_logistics_money)
        TextView order_logistics_money;
    }
}
